<?php
/* 
 * 核心类
 * 
*/

class Kernel {
    private static $_imported = array();
    /**
     *
     * 导入类文件
     */
    static function import($path) {
        $real_path = FRAMEWORK_ROOT.str_replace('.', '/', $path);
        $real_file_path = $real_path.'.php';
        if(is_file($real_file_path)) {
            if(!isset(self::$_imported[$real_path])) {
                require_once($real_file_path);
                self::$_imported[$real_path] = true;
            }
        }else if(is_dir($real_path)) {
            if(!isset(self::$_imported[$real_path])) {
                $dir_handle = opendir($real_path);
                while($file_in_dir = readdir($dir_handle)) {
                    if(substr($file_in_dir, -4) == '.php') {
                        require_once($real_path.'/'.$file_in_dir);
                    }
                }
                self::$_imported[$real_path] = true;
            }
        }
    }

    /**
     *
     * 获取url
     */
    static function url($controller,$action,$params = null) {
        $url = FRAMEWORK_WEB_ROOT.FRAMEWORK_INDEX_FILE."?controller=$controller&action=$action";
        if(!empty($params)) {
            $params_str = '&';
            foreach($params as $k => $v) {
                $params_str .= sprintf('%s=%s&',urlencode($k),urlencode($v));
            }
            $params_str = substr($params_str, 0,-1);
            $url .= $params_str;
        }
        return $url;
    }

    /**
     *
     * 解析请求
     */
    static function parseRequest(){
//        $query_string = $_SERVER['QUERY_STRING'];
        /*if(empty($query_string)){
            return false;
        }*/
        $request = array();
//        $params = explode('&', $query_string);
        $params_ret = $_REQUEST;
//        $index = 0;
//        foreach($params as $param){
//            $pos = strpos($param, '=');
//            if($pos === false){
//                $params_ret[$index] = $param;
//                ++$index;
//            }else{
//                $params_ret[substr($param, 0,$pos)] = substr($param, $pos + 1);
//            }
//        }
        if(isset($params_ret['controller'])){
            $request['controller'] = $params_ret['controller'];
//            unset($params_ret['controller']);
        }else{
            $request['controller'] = FRAMEWORK_DEFAULT_CONTROLLER;
        }
        if(isset($params_ret['action'])){
            $request['action'] = $params_ret['action'];
//            unset($params_ret['action']);
        }else{
            $request['action'] = FRAMEWORK_DEFAULT_ACTION;
        }
//        $request['params'] = $params_ret;
        return $request;
    }

    /**
     *
     * 调用请求的action
     */
    static function callAction($controller,$action,$params = array()){
        if(!class_exists($controller)){
            self::import('controller.'.strtolower($controller));
            if(!class_exists($controller)){
                throw new FWException(404);
            }
        }
        $c = new $controller();
        if(!method_exists($c,$action)){
            throw new FWException(404);
        }
        return call_user_func_array(array($c,$action),$params);
    }

    /**
     * 执行action
     */
    static function doCA(){
      ob_start();
      try{
      $request = self::parseRequest();
        if(empty($request['params'])){
            $ret = self::callAction($request['controller'], $request['action']);
        }else{
            $ret = self::callAction($request['controller'], $request['action'],$request['params']);
        }
        }catch(FWException $e){
            //异常处理
            $code = $e->getCode();
            switch($code){
                case 404:
                    self::show404();
                    break;
            }
        }
        ob_end_flush();
        return $ret;
    }

    /**
     * view
     * 
     */
    static  function render($view,$data = array()){
        $url = FRAMEWORK_ROOT.'view/'.$view.'.html';
        include($url);
    }

    /**
     *跳转
     * 
     */
    static function redirect($url){
        header("Location: $url");
    }

    static function redirectTo($controller,$action,$params=array()){
        $url = self::url($controller, $action, $params);
        self::redirect($url);
    }

    static function show404($url = null){
        if($url != null){
            self::redirect($url);
        }else if(defined('FRAMEWORK_PAGE_NOTFOUND')){
            self::redirect(FRAMEWORK_PAGE_NOTFOUND);
        }else{
            echo <<<HTML
   <html>
       <head>
       <title>The Page Was Not Found!</title>
       </head>
       <body>
       <H1>The Page Was Not Found!</H1>
       </body>
   </html>
HTML;
        }
    }
}
?>
